<?php

namespace app\data\command;

use app\admin\service\WxWorkService;
use app\data\service\CityUserRelationService;
use app\data\service\MessageParseService;
use app\data\service\RecordService;
use app\data\service\StringParse;
use think\admin\Command;
use think\admin\Exception;
use think\console\Input;
use think\console\Output;
use think\db\exception\DbException;
use think\facade\Log;

/**
 * 聊天记录解析
 * Class MessageParse
 * @package app\data\command
 */
class MessageParse extends Command
{
    protected function configure()
    {
        $this->setName('message_parse');
        $this->setDescription('聊天记录解析');
    }

    /**
     * 采集操作
     * @param Input $input
     * @param Output $output
     * @return void
     * @throws Exception
     * @throws DbException
     */
    protected function execute(Input $input, Output $output)
    {
        while (true) {
            $table = 'message_group';

            foreach ($this->app->db->name($table)->where('is_deal', 0)->cursor() as $vo) try {

                if (!empty($vo['content']) && $vo['message_type'] == 'text') {

                    $vo['text'] = MessageParseService::instance()->translationText($vo['content'], $vo['mentions']);

                    $isDone = false;

                    $message = StringParse::instance()->manualEntry($vo['text']);

                    // 手动录入(必须匹配到城市负责人才会手动录入)
                    if (!empty($message['has_uuid_city']) && !empty($message['phone']) && !empty($message['plate_number'])) {

                        if (isset($message['paint_status']) && $message['paint_status'] === '1') {
                            if (!isset($message['car_vehicle_type_id'])) {
                                $message['car_vehicle_type_id'] = 0;
                            }
                            if (!isset($message['end_paint_status'])) {
                                $message['end_paint_status'] = -1;
                            }
                            $message = CityUserRelationService::instance()->calcPaintPrice([], $message);
                        }

                        MessageParseService::instance()->manualEntry($vo['text'], $message);
                        $isDone = true;
                    }

                    // 信息采集(车牌不为空)
                    if (!$isDone) {
                        $message = StringParse::instance()->carParse($vo['text']);
                        if (!empty($message['plate_number']) && !empty($message['phone'])) {
                            MessageParseService::instance()->carParse($vo['text'], $message);
                            $isDone = true;
                        }
                    }

                    // 自动结单
                    if (!$isDone) {
                        $message = StringParse::instance()->completeInfo($vo['text']);
                        if (!empty($message['plate_number']) && (
                                $message['paint_status'] !== -1 ||
                                $message['end_paint_status'] !== -1 ||
                                $message['ad_cover_fee'] !== -1 ||
                                $message['has_cover'] !== -1 ||
                                $message['is_deal'] !== -1 ||
                                $message['cover_num'] !== '' ||
                                $message['cover_price'] !== '' ||
                                $message['painter'] !== '' ||
                                $message['car_vehicle_type'] !== ''
                            )) {
                            MessageParseService::instance()->completeInfo($message);
                            $isDone = true;
                        }
                    }

                    // 保存邀约情况
                    if (!$isDone) {
                        MessageParseService::instance()->inviteInfo($vo['text']);
                    }
                }

                $this->app->db->name($table)->where('id', $vo['id'])->delete();
            } catch (\Exception $exception) {
                $this->app->db->name($table)->where('id', $vo['id'])->update(['is_deal' => 2]);
                Log::error("处理聊天记录 {$vo['id']} 失败: " . json_encode([
                        'message' => $exception->getMessage(),
                        'file' => $exception->getFile(),
                        'line' => $exception->getLine(),
                        'trace' => $exception->getTraceAsString(),
                    ]));
            }

            sleep(1);
        }
    }

}